﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bxas.Utility.Web.Keepers;
using PhotoHsn.BLL.Authantication;
using PhotoHsn.BLL.Adapters;
using PhotoHsn.WebApp.Core;
using PhotoHsn.BLL.Core;
using PhotoHsn.BLL;
using Bxas.Core.Exceptions;

namespace PhotoHsn.Web
{
    public partial class Login1 : BasePage
    {
  
             protected void Page_Load(object sender, EventArgs e)
        {
            this.Form.DefaultButton = btnSender.UniqueID;
            if (!IsPostBack)
            {
                page_load_not_post_back();
            }
        }

        #region LOGIN                                                                       .

        private void page_load_not_post_back()
        {
            //sonra google istatistik ekleriz.
            //if (!this.Page.ClientScript.IsClientScriptBlockRegistered("googlelog"))
            //{
            //    string val = IslemMerkeziBLL.ConfigurationMng.getConfigurationItemValue(conStr, IslemMerkezi.Tools.Resources.ConfigurationGroupCodes.CommonAyarlar, "GoogleLogScript");
            //    string googlelogScript = CommonOperations.StringOperations.Operations.get_google_log_script(val);
            //    this.Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "googlelog", googlelogScript.ToString());
            //}

            string userName = string.Empty, pass = string.Empty;
            Bxas.Utility.Cookie.UserCookies.read_cookie("unCk", ref userName);
            Bxas.Utility.Cookie.UserCookies.read_cookie("upCk", ref pass);

            if (!string.IsNullOrEmpty(userName))
                txtUserName.Text = userName;
            cBoxUserName.Checked = userName != string.Empty;
            cBoxPass.Checked = pass != string.Empty;
            if (pass != "") login_operation(pass);
        }

        private void login_operation(string pass)
        {
           
            try
            {
                SessionKeeper.clear_all();

                if (string.IsNullOrEmpty(pass))
                    pass = Bxas.Utility.Security.MD5Operations.convert_string_to_MD5(txtPass.Text);

                MembersBs memberBs = new MembersBs();
                Members member = new Members();
                Int32 memberStatus = memberBs.get_for_login(txtUserName.Text, pass, ref member);
                if (memberStatus == EnumMemberStatus.Active)
                {
                    if (cBoxUserName.Checked)
                    {
                        Bxas.Utility.Cookie.UserCookies.remove_cookie("unCk");
                        Bxas.Utility.Cookie.UserCookies.add_cookie("unCk", txtUserName.Text, 50);
                    }
                    if (cBoxPass.Checked)
                    {
                        Bxas.Utility.Cookie.UserCookies.remove_cookie("upCk");
                        Bxas.Utility.Cookie.UserCookies.add_cookie("upCk", pass, 50);
                    }

                    MemberUser memberUser = new MemberUser();
                    memberUser.Member = member;

                    RolesBs roleBs = new RolesBs();
                    memberUser.MenuRoles = roleBs.get_for_authority(member.Id, EnumRolesTypes.Menu);
                    memberUser.FolderRoles = roleBs.get_for_authority(member.Id, EnumRolesTypes.Folder);
                    memberUser.PageRoles = roleBs.get_for_authority(member.Id, EnumRolesTypes.Page);
                    memberUser.ProcessingRoles = roleBs.get_for_authority(member.Id, EnumRolesTypes.Processing);

                    SessionKeeper.add_is_not_contain(SessionNames.MemberUser, memberUser);

                    Response.Redirect("~/Default.aspx");
                }               
            }
            catch (BxasCoreException ex)
            {
                MessageShow(new BxasException(this, ex.Message, ex));
            }
          
        }

     

      

        protected void lnkBtnSubmit_Click(object sender, EventArgs e)
        {
            login_operation(null);
        }

        #endregion

        #region REMEMBER PASSWORD                                                           .

        protected void lnkBtnSend_Click(object sender, EventArgs e)
        {
            //Users intranetUser = new Users();
            //intranetUser.Where.IsDeleted.Value = false;
            //intranetUser.Where.IsActive.Value = true;
            //intranetUser.Where.IsLockedByBase.Value = false;

            //XrmSystemUsers xrmSystemUser = new XrmSystemUsers();
            //xrmSystemUser.Where.Email1.Value = txtEmail.Text;
            //xrmSystemUser.Where.IsActive.Value = true;
            //xrmSystemUser.Where.IsDeleted.Value = false;

            //if (xrmSystemUser.Query.Load())
            //{
            //    intranetUser.Where.XrmSystemUserId.Value = xrmSystemUser.XrmSystemUserId;
            //    intranetUser.Where.UserType.Value = "U";
            //}
            //else
            //{
            //    CstmContacts cstmContacts = new CstmContacts();
            //    cstmContacts.Where.Email.Value = txtEmail.Text;
            //    cstmContacts.Where.IsActive.Value = true;
            //    cstmContacts.Where.IsDeleted.Value = false;

            //    if (cstmContacts.Query.Load())
            //    {
            //        intranetUser.Where.CstmContactId.Value = cstmContacts.CstmContactId;
            //        intranetUser.Where.UserType.Value = "C";
            //    }
            //    else
            //    {
            //        GiveMessage(IslemMerkezi.Tools.Resources.Alerts.Common.V103, true);
            //        return;
            //    }
            //}

            //if (intranetUser.Query.Load())
            //{
            //    string create_password = CommonOperations.StringOperations.PasswordGenerator.get_password();
            //    string hashed_password = CommonOperations.Security.MD5Operations.convert_string_to_MD5(create_password);

            //    TransactionMgr trn = TransactionMgr.ThreadTransactionMgr();
            //    trn.BeginTransaction();

            //    try
            //    {
            //        intranetUser.PassExpireDate = DateTime.Now;
            //        intranetUser.Password = hashed_password;
            //        intranetUser.Save();

            //        string conStr = ConfigurationManager.AppSettings["dbConnection"];

            //        string fromemail = ConfigurationMng.getConfigurationItemValue(conStr, IslemMerkezi.Tools.Resources.ConfigurationGroupCodes.EmailAyarlari, "IslemmerkeziEmail");
            //        string fromename = ConfigurationMng.getConfigurationItemValue(conStr, IslemMerkezi.Tools.Resources.ConfigurationGroupCodes.CommonAyarlar, "PlatformNameIng");
            //        string datastore = ConfigurationMng.getConfigurationItemValue(conStr, IslemMerkezi.Tools.Resources.ConfigurationGroupCodes.DataConfig, "DataStore");

            //        Dictionary<string, string> paramValues = new Dictionary<string, string>();
            //        paramValues.Add("KULLANICI_ADI", intranetUser.UserName);
            //        paramValues.Add("SIFRE", create_password);

            //        string content_body = CommonOperations.Message.TemplateReader.InitializeBody(
            //                            datastore + "EmailTemplates\\SendUserPassword.htm",
            //                            paramValues);

            //        if (string.IsNullOrEmpty(content_body))
            //        {
            //            GiveMessage(IslemMerkezi.Tools.Resources.Alerts.Common.H100, true);
            //            trn.RollbackTransaction();
            //            TransactionMgr.ThreadTransactionMgrReset();
            //            return;
            //        }

            //        Dictionary<string, string> paramMainValues = new Dictionary<string, string>();
            //        paramMainValues.Add("HEADER", "İlerimed E-İş Platformu Kullanıcı Bilgileri");
            //        paramMainValues.Add("CONTENT_HTML", content_body);

            //        string email_body = CommonOperations.Message.TemplateReader.InitializeBody(
            //                            datastore + "EmailTemplates\\Master.htm",
            //                            paramMainValues);

            //        if (string.IsNullOrEmpty(email_body))
            //        {
            //            GiveMessage(IslemMerkezi.Tools.Resources.Alerts.Common.H100, true);
            //            trn.RollbackTransaction();
            //            TransactionMgr.ThreadTransactionMgrReset();
            //            return;
            //        }

            //        List<System.Net.Mail.MailAddress> ToList = new List<System.Net.Mail.MailAddress>();
            //        List<System.Net.Mail.MailAddress> BccList = new List<System.Net.Mail.MailAddress>();
            //        List<System.Net.Mail.MailAddress> CcList = new List<System.Net.Mail.MailAddress>();
            //        System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress(fromemail, fromename);
            //        ToList.Add(new System.Net.Mail.MailAddress(xrmSystemUser.Email1, xrmSystemUser.FullName));

            //        string err = "";

            //        if (!CommonOperations.Message.MailOperation.SendMail(true, ToList, from, BccList, CcList, "Kullanici Islemleri", email_body, ref err))
            //        {
            //            GiveMessage(IslemMerkezi.Tools.Resources.Alerts.Common.H100, true);
            //            trn.RollbackTransaction();
            //            TransactionMgr.ThreadTransactionMgrReset();
            //        }
            //        else
            //        {
            //            trn.CommitTransaction();
            //            GiveMessage(IslemMerkezi.Tools.Resources.Alerts.Administration.B100, false);
            //        }
            //    }
            //    catch
            //    {
            //        GiveMessage(IslemMerkezi.Tools.Resources.Alerts.Common.V103, true);
            //        trn.RollbackTransaction();
            //        TransactionMgr.ThreadTransactionMgrReset();
            //    }
            //}
            //else
            //    GiveMessage(IslemMerkezi.Tools.Resources.Alerts.Common.V103, true);
        }


        #endregion
        }
    
}